﻿using System;

namespace DuckFlyWithInterface
{
    abstract class Duck
    {
        public string Name { get; set; }
        public void Display()
        {
            Console.WriteLine("这是一只{0}", Name);
        }
        // 飞行行为相关
        public IFlyBehavior flyBehavior { get; set; }
        public void performFly()
        {
            flyBehavior.Fly();
        }
        public void changeFlyBehavior(IFlyBehavior fl)
        {
            flyBehavior = fl;
        }

        // 叫声行为相关
        public IQuackBehavior quackBehavior { get; set; }
        public void performQuack()
        {
            quackBehavior.Quack();
        }
        public void changeQuackBehavior(IQuackBehavior qb)
        {
            quackBehavior = qb;
        }

        // 游泳行为相关
        public ISwimBehavior swimBehavior { get; set; }
        public void performSwim()
        {
            swimBehavior.Swim();
        }
        public void changeSwimBehavior(ISwimBehavior sb)
        {
            swimBehavior = sb;
        }

    }

    class Shelduck : Duck
    {
        public Shelduck()
        {
            Name = "Shelduck";
            flyBehavior = new FlyWithWings();
            quackBehavior = new NormalQuack();
            swimBehavior = new NormalSwim();
        }
    }

    class RedheadDuck : Duck
    {
        public RedheadDuck()
        {
            Name = "RedheadDuck";
            flyBehavior = new FlyWithWings();
            quackBehavior = new LoudQuack();
            swimBehavior = new FastSwim();
        }
    }

    class RobberDuck : Duck
    {
        public RobberDuck()
        {
            Name = "RobberDuck";
            flyBehavior = new FlyWithNoWay();
            quackBehavior = new Squeak();
            swimBehavior = new FloatOnly();
        }
    }
}


namespace DuckFlyWithInterface
{
    interface IFlyBehavior
    {
        public void Fly();
    }

    public class FlyWithWings : IFlyBehavior
    {
        public void Fly()
        {
            Console.WriteLine("用翅膀飞！");
        }
    }
    public class FlyWithNoWay : IFlyBehavior
    {
        public void Fly()
        {
            Console.WriteLine("不能飞！");
        }
    }
    public class FlyWithRocket : IFlyBehavior
    {
        public void Fly()
        {
            Console.WriteLine("乘坐火箭飞！");
        }
    }
}


namespace DuckFlyWithInterface
{
    /// <summary>
    /// 鸭子叫声行为的接口
    /// </summary>
    interface IQuackBehavior
    {
        public void Quack();
    }

    /// <summary>
    /// 正常嘎嘎叫的实现
    /// </summary>
    public class NormalQuack : IQuackBehavior
    {
        public void Quack()
        {
            Console.WriteLine("嘎嘎嘎！");
        }
    }

    /// <summary>
    /// 吱吱叫的实现（如橡皮鸭）
    /// </summary>
    public class Squeak : IQuackBehavior
    {
        public void Quack()
        {
            Console.WriteLine("吱吱吱！");
        }
    }

    /// <summary>
    /// 不会叫的实现（如木头鸭）
    /// </summary>
    public class MuteQuack : IQuackBehavior
    {
        public void Quack()
        {
            Console.WriteLine("（沉默中，不会叫）");
        }
    }

    /// <summary>
    /// 大声叫的实现
    /// </summary>
    public class LoudQuack : IQuackBehavior
    {
        public void Quack()
        {
            Console.WriteLine("嘎嘎嘎！（声音特别大）");
        }
    }
}


namespace DuckFlyWithInterface
{
    /// <summary>
    /// 鸭子游泳行为的接口
    /// </summary>
    interface ISwimBehavior
    {
        public void Swim();
    }

    /// <summary>
    /// 正常游泳的实现（大多数真实鸭子）
    /// </summary>
    public class NormalSwim : ISwimBehavior
    {
        public void Swim()
        {
            Console.WriteLine("自由自在地游泳！");
        }
    }

    /// <summary>
    /// 只能漂浮的实现（如橡皮鸭）
    /// </summary>
    public class FloatOnly : ISwimBehavior
    {
        public void Swim()
        {
            Console.WriteLine("只能漂浮，不会真正游泳！");
        }
    }

    /// <summary>
    /// 沉底的实现（如石头鸭）
    /// </summary>
    public class Sink : ISwimBehavior
    {
        public void Swim()
        {
            Console.WriteLine("无法浮在水面，会沉下去！");
        }
    }

    /// <summary>
    /// 快速游泳的实现（如野鸭）
    /// </summary>
    public class FastSwim : ISwimBehavior
    {
        public void Swim()
        {
            Console.WriteLine("快速地划水前进！");
        }
    }
}

namespace DuckFlyWithInterface
{
    class Program
    {
        static void Main(string[] args)
        {
            Duck duck1 = new Shelduck();
            Duck duck2 = new RedheadDuck();
            Duck duck3 = new RobberDuck();

            Console.WriteLine("=== 麻鸭 ===");
            duck1.Display();
            duck1.performFly();
            duck1.performQuack();
            duck1.performSwim();
            Console.WriteLine();

            Console.WriteLine("=== 红头鸭 ===");
            duck2.Display();
            duck2.performFly();
            duck2.performQuack();
            duck2.performSwim();
            Console.WriteLine();

            Console.WriteLine("=== 橡皮鸭 ===");
            duck3.Display();
            duck3.performFly();
            duck3.performQuack();
            duck3.performSwim();
            Console.WriteLine();

            Console.WriteLine("=== 动态改变橡皮鸭的行为 ===");
            duck3.changeFlyBehavior(new FlyWithRocket());
            duck3.changeQuackBehavior(new LoudQuack());
            duck3.changeSwimBehavior(new FastSwim());

            duck3.Display();
            duck3.performFly();
            duck3.performQuack();
            duck3.performSwim();
        }
    }
}